package com.boot.config;

import io.swagger.annotations.Api;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Profile;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class Swagger2Config {

    //默认dev环境
    @Profile({"default", "dev"})
    @Bean
    public Docket createWebApi() {
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfoDev()).select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class)).paths(PathSelectors.any()).build();
    }

    //test环境接口访问地址:http://localhost:6868/swagger-ui.html
    @Profile("test")
    @Bean
    public Docket createWebApiDev() {
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfoDev()).select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class)).paths(PathSelectors.any()).build();
    }

    //pro环境接口访问地址:http://localhost:6868/swagger-ui.html
    @Profile("pro")
    @Bean
    public Docket createWebApiTest() {
        return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfoDev()).select()
                .apis(RequestHandlerSelectors.withClassAnnotation(Api.class)).paths(PathSelectors.any()).build();
    }

    private ApiInfo apiInfoDev() {
        return new ApiInfoBuilder()
                .title("xxx测试项目标题")
                .description("xxx接口描述")
                //联系人实体类
                .contact(new Contact("xxx名字", "xxx网址", "xxx邮箱"))
                //版本号
                .version("1.0.0-SNAPSHOT")
                .build();
    }
}